package so.shanku.essential.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import cn.jpush.android.api.JPushInterface;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.json.JsonMap;
import net.tsz.afinal.json.JsonParseHelper;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.getdata.GetDataConfing;
import so.shanku.essential.getdata.GetDataQueue;
import so.shanku.essential.getdata.IGetServicesDataCallBack;
import so.shanku.essential.getdata.ShowGetDataError;
import so.shanku.essential.utils.ExtraKeys;
import so.shanku.essential.utils.StringUtil;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Jaelyn on 2015/9/15.
 */
public class UserChangePhoneFristActivity extends BaseUIActivity {
	/**
	 * 手机号码
	 */
	@ViewInject(id = R.id.user_account_ed)
	private EditText user_account_ed;
	/**
	 * 密码
	 */
	@ViewInject(id = R.id.user_pwd_ed)
	private EditText user_pwd_ed;
	/**
	 * 下一步
	 */
	@ViewInject(id = R.id.btn_next, click = "btn_next_click")
	private TextView btn_next;
	private String userPhone;
	private String userPassword;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_change_phone_first);
		initActivity(true, R.drawable.back, null, R.string.change_phone, -1, null);
	}

	/**
	 * 登录按钮
	 */
	public void btn_next_click(View view) {
        if (userLoginJudge()) {
            getData_ChangePhoneFirst();
        }

	}

	public void getData_ChangePhoneFirst(){
		loadingToast.show();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("UserAccount", userPhone);
		params.put("Password", userPassword);
		params.put("Tag", JPushInterface.getRegistrationID(this));
		GetDataQueue queue = new GetDataQueue();
		queue.setCallBack(callBack);
		queue.setActionName(GetDataConfing.GetAction_CheckLogin);
		queue.setWhat(GetDataConfing.what_CheckLogin);
		queue.setParamsNoJson(params);
		getDataUtil.getData(queue);
	}
	/**
	 * ��ȡ���������ݵķ�����Ϣ
	 */
	private IGetServicesDataCallBack callBack = new IGetServicesDataCallBack() {

		@Override
		public void onLoaded(GetDataQueue entity) {
			if (entity.isOk()) {
				JsonMap<String, Object> data = JsonParseHelper.getJsonMap(entity.getInfo());
				if (ShowGetDataError.isOkAndCodeIsNot1(UserChangePhoneFristActivity.this,
						entity.getInfo())) {
					if (entity.what == GetDataConfing.what_CheckLogin) {
						Intent intent=new Intent(UserChangePhoneFristActivity.this,UserChangePhoneSecondActivity.class);
					    intent.putExtra(ExtraKeys.Key_Msg1,true);
					    startActivityForResult(intent, 1);
					}
				}
			} else {
				ShowGetDataError.showNetError(UserChangePhoneFristActivity.this);
			}
			loadingToast.dismiss();
		}
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode==RESULT_OK){
			UserChangePhoneFristActivity.this.finish();
		}
	}

	/**
	 * 密码判断
	 */
	public boolean isPasswordAuthentication(String password) {
		if(!StringUtil.passwordAuthenticationLength(password)){
			toast.showToast(R.string.register_passwordRule);
			return false;
		} else if (!StringUtil.passwordAuthentication(password)){
			toast.showToast(R.string.password_format_error);
			return false;
		}
		return true;
	}

	/**
	 * 判断手机号码
	 */

	public boolean judgeMobileNo() {
		boolean isjudge = true;
		userPhone = user_account_ed.getText().toString();
		if (userPhone == null || "".equals(userPhone)) {
			//手机号码为空的情况
			toast.showToast(R.string.register_name_null);
			isjudge = false;
		} else if (userPhone.length() < 11) {
			toast.showToast(R.string.phone_formal_error);
			isjudge = false;
		}
		return isjudge;
	}

	/**
	 * 登录判断
	 */
	public boolean userLoginJudge() {
		boolean isjudge = true;
		boolean password = true;
		userPassword = user_pwd_ed.getText().toString();
		if (userPassword == null || "".equals(userPassword)) {
			toast.showToast(R.string.register_password_null);
			password = false;
		} else {
			password = isPasswordAuthentication(userPassword);
		}
        isjudge = judgeMobileNo() && password;
		return isjudge;
	}

}
